﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SimplePaginationDemo
{
    public partial class Information : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BindInfo(page,pagesize);
        }
        int  count;
        public void BindInfo(int page,int pagesize)
        {
            string Scon = ConfigurationManager.ConnectionStrings["teaching"].ToString();
            using (SqlConnection con = new SqlConnection(Scon)) 
            {
                con.Open();
                //string sql = string.Format(@"select top {0} o.* from (select row_number() over(order by teacherno) as rownumber,* from(select * from teacher) as oo) as o where rownumber>0",pagesize);
                string sql=string.Format(@"select * from(select ROW_NUMBER()over(order by teacherno)as A ,* from teacher where 1=1 )b where  b.A between  {0} and {1}",(page - 1) * pagesize+1, page * pagesize);//sql分页查询
                using (SqlCommand com = new SqlCommand(sql, con))
                {
                     using (SqlDataReader read = com.ExecuteReader())
                     {
                         GridView1.DataSource = read;
                         GridView1.DataBind();
                     }
                }
               
            } 
        }

       public int pagesize = 2;
       private int _page;

       public int page
       {
           get
           {
               try
               {
                   _page = Request.QueryString["page"] == null ? 1 : Convert.ToInt32(Request.QueryString["page"]);
               }
               catch (Exception e)
               {
                   _page = 1;

               }
              
               return _page;
           }
           set { _page = value; }
       }

       public string GetPagination()
       {
           string Scon = ConfigurationManager.ConnectionStrings["teaching"].ToString();
           using (SqlConnection con = new SqlConnection(Scon))
           {
               con.Open();
               string scount = "select count(*) from teacher";
               using (SqlCommand com = new SqlCommand(scount, con))
               {
                   count = (int)com.ExecuteScalar();
               }
           }
           string s=pagination.GetPage(page,pagesize,count);
           return s;
       }
       
    }
}